package babo.cd.physicalexperiment.activity;

import android.content.SharedPreferences;
import android.text.TextUtils;
import android.view.View;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import babo.cd.physicalexperiment.BaseActivity;
import babo.cd.physicalexperiment.MainActivity;
import babo.cd.physicalexperiment.app.MyApplication;
import babo.cd.physicalexperiment.bean.LoginBean;
import babo.cd.physicalexperiment.bean.UserInfoBean;
import babo.cd.physicalexperiment.data.MyEnventData;
import babo.cd.physicalexperiment.data.UserInfo;
import babo.cd.physicalexperiment.databinding.ActivityLoginBinding;
import babo.cd.physicalexperiment.utils.OKCallback;
import babo.cd.physicalexperiment.utils.OKCallback2;
import babo.cd.physicalexperiment.utils.OkUtils;
import babo.cd.physicalexperiment.utils.Out;
import babo.cd.physicalexperiment.utils.URLS;
import okhttp3.Credentials;

public class LoginActivity extends BaseActivity {


    private ActivityLoginBinding binding;
    @Override
    protected View bindView() {
        binding = ActivityLoginBinding.inflate(getLayoutInflater());
        return binding.getRoot();

    }

    @Override
    protected void initView() {


    }

    @Override
    public void setListener() {
        super.setListener();
        binding.txZhuce.setOnClickListener(view -> {
            JumpAct(context,RegisterActivity.class);
        });
        binding.loginIp.setOnClickListener(view -> {
            JumpAct(context,MainActivity.class);
        });
        binding.loginBtn.setOnClickListener(v -> {
           if (TextUtils.isEmpty(binding.usernameEt.getText().toString().trim())||TextUtils.isEmpty(binding.passwordEt.getText().toString().trim())){
               Out.toast(context,"请输入用户名和密码！");
               return;
           }
           doLogin();
        });
    }

    private void doLogin() {

      String bck=  Credentials.basic(binding.usernameEt.getText().toString().trim(),binding.passwordEt.getText().toString().trim());
      Out.out("bck=="+bck);
        OkUtils.GetOkBacik(URLS.login, new OKCallback2(context) {
            @Override
            public void Sucess(String str) {
                super.Sucess(str);
                Out.out("进来成功AA=="+str);
                LoginBean loginBean=gson.fromJson(str,LoginBean.class);
                UserInfo.JsessionId=loginBean.getData().getToken();
                UserInfo.userId=loginBean.getData().getUserId();
                SharedPreferences.Editor editor=sharedPreferences.edit();
                editor.putString("uid",UserInfo.userId);
                editor.putString("phone",binding.usernameEt.getText().toString().trim());
                editor.putString("psw",binding.passwordEt.getText().toString().trim());
                editor.putString("token",UserInfo.JsessionId);
                editor.commit();
                getUserInfo();
            }

            @Override
            public void Eorror(int code) {
                super.Eorror(code);
                if (code==401){
                    Out.toast(context,"用户名或密码错误，请重新输入！");
                }
            }
        },bck);
    }

    private void getUserInfo() {
        OkUtils.GetOk(URLS.getUserInfo,new OKCallback(context){
            @Override
            public void Sucess(String str) {
                super.Sucess(str);
                UserInfoBean userInfoBean=gson.fromJson(str,UserInfoBean.class);
                MyApplication.getInstance().setUserBaseInfo(userInfoBean.getData());
                if (TextUtils.isEmpty(userInfoBean.getData().getName())){
                    goToNextActivity(UploadBaseInfoActivity.class);
                    finish();
                }else{
                    goToNextActivity(MainActivity.class);
                    finish();
                }


            }
        });
    }

    @Override
    protected void loadData() {
        String phone=sharedPreferences.getString("phone","");
        if (!TextUtils.isEmpty(phone)){
            binding.usernameEt.setText(phone);
        }
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetMessage(MyEnventData message) {
        String msg=message.message;
        if (msg.startsWith("phone")){
            binding.usernameEt.setText(msg.replace("phone",""));
        }

    }
}
